﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace Directory.Web
{
	public partial class Admin_CategorySecurity : System.Web.UI.Page
	{
		protected string categoryIdKey = "categoryId";
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				CascadingCategory.DataSource = CategoryDataProxy.GetChildsOfCategory(WebSettings.GetInstance().RootCategoryId);
				CascadingCategory.DataBind();

				BindUser();
			}
		}
		// 保存修改
		protected void Button1_Click(object sender, EventArgs e)
		{
			if (CascadingCategory.SelectedId > 0)
			{
				int totalRecords;
				IList<Directory.Model.CategoryUser> users = CategoryDataProxy.GetUsersForCategory(CascadingCategory.SelectedId, 0, 999, out totalRecords);
				foreach (ListItem item in cblUser.Items)
				{
					if (item.Selected && GetIndexOfCategoryUser(item.Value, users) < 0)
					{
						CategoryDataProxy.AddUserToCategory(item.Value, CascadingCategory.SelectedId);
					}
					else if (!item.Selected && GetIndexOfCategoryUser(item.Value, users) >= 0)
					{
						CategoryDataProxy.RemoveUserFromCategory(item.Value, CascadingCategory.SelectedId);
					}
				}
			}
			BindUser();
			BindCategory();
		}

		protected void btnLoadUsers_Click(object sender, EventArgs e)
		{
			BindUser();
			BindCategory();
		}

		private void BindUser()
		{
			cblUser.Items.Clear();

			if (CascadingCategory.SelectedId > 0)
			{
				int totalRecords;
				string[] allUsers = RoleDataProxy.GetUsersInRole(WebSettings.GetInstance().CategoryAdminRole, 0, 999, out totalRecords);
				IList<Directory.Model.CategoryUser> users = CategoryDataProxy.GetUsersForCategory(CascadingCategory.SelectedId, 0, 999, out totalRecords);
				foreach (string usr in allUsers)
				{
					ListItem item = new ListItem(usr, usr);
					int index = GetIndexOfCategoryUser(usr, users);
					if (index >= 0)
					{
						item.Selected = true;
						item.Enabled = !users[index].IsPermitInherited;
					}
					else
					{
						item.Selected = false;
						item.Enabled = true;
					}
					cblUser.Items.Add(item);
				}
			}
		}

		private int GetIndexOfCategoryUser(string username, IList<Directory.Model.CategoryUser> list)
		{
			if (list == null)
			{
				return -1;
			}
			if (username == null)
			{
				return -1;
			}
			username = username.Trim().ToLower();
			if (username.Length == 0)
			{
				return -1;
			}
			foreach (Directory.Model.CategoryUser cu in list)
			{
				if (cu.UserName.ToLower() == username)
				{
					return list.IndexOf(cu);
				}
			}
			return -1;
		}

		private void BindCategory()
		{
			IList<Directory.Model.CategoryInfo> parentPath = CategoryDataProxy.GetRecursiveParentsOfCategory(CascadingCategory.SelectedId);
			string pathStr = null;
			foreach (Directory.Model.CategoryInfo cat in parentPath)
			{
				pathStr += cat.CategoryId.ToString() + ",";
			}
			if (pathStr != null)
			{
				CascadingCategory.SelectedIndexStr = pathStr.Remove(pathStr.Length - 1);
				CascadingCategory.AutoLoad = true;
			}
		}
	}
}